package com.cango.qutz.web.auth.resource.controller;

import com.cango.framework.base.AjaxReturnVO;
import com.cango.framework.base.controller.BaseController;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.resource.service.IResourceService;
import com.cango.qutz.web.entity.ResourceEntity;
import com.cango.qutz.web.entity.ResourceEntity;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * <p>ClassName: ResourceController</p>
 * <p>Description: 系统资源Controller</p>
 * <p>Author: weih</p>
 * <p>Date: 2015-03-20</p>
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {

	/**
     * log
     */
    private final Log log = LogFactory.getLog(ResourceController.class);
    
    /**
     * <p>Field resourceService: 系统资源Service注入</p>
     */
    @Autowired
    private IResourceService resourceService;
    
    /**
     * <p>Description: 跳转系统资源新增页面</p>
     */
    @RequestMapping("/toAddResource")
    public String toAddResource(){
        return "resource/resourceAdd";
    }
    /**
     * <p>Description: 跳转系统资源列表页面</p>
     * @param resource  系统资源对象
     * @return
     */
    @RequestMapping("/list")
    public String list(ResourceEntity resource){
        return "resource/resourceList";
    }
    
    /**
     * <p>Description: 分页查询系统资源记录</p>
     * @param resource 系统资源对象
     * @param response response
     * @param pageInfo 分页对象
     */
    @RequestMapping("/getListData")
    public void getListData(ResourceEntity resource,HttpServletResponse response,PageInfo pageInfo){
        PaginationResult<ResourceEntity> result=resourceService.queryListByPage(resource, pageInfo);
        super.ajaxJson(response, result);
    }
    /**
     * <p>Description: 保存系统资源记录</p>
     * @param resource 系统资源对象
     * @param response
     */
    @RequestMapping("/saveResource")
    public void saveResource(ResourceEntity resource,HttpServletResponse response){
        log.info("用户"+getSessionUser().getUserName()+"新增了系统资源:"+(new Gson()).toJson(resource));
    	resource.setCreateUser(getSessionUser().getUserId());
        resource.setCreateTime(new Date());
        resourceService.insert(resource);
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    /**
     * <p>Description: 跳转系统资源编辑页面</p>
     * @param resource 系统资源对象
     * @param model
     * @return
     */
    @RequestMapping("toEditResource")
    public String toEditResource(ResourceEntity resource, Model model){
        model.addAttribute("resourceCode",resource.getResourceCode());
        return "resource/resourceEdit";
    }
    
   /**
     * <p>Description: 保存系统资源修改记录</p>
     * @param resource 系统资源对象
     * @param response
     */
    @RequestMapping("/editResource")
    public void editResource(ResourceEntity resource, HttpServletResponse response){
    	log.info("用户"+getSessionUser().getUserName()+"修改了系统资源:"+(new Gson()).toJson(resource));
    	resource.setUpdateUser(getSessionUser().getUserId());
        resource.setUpdateTime(new Date());
        resourceService.update(resource);
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    /**
     * <p>Description: 得到单个系统资源记录</p>
     * @param resource 系统资源对象
     * @param response
     */
    @RequestMapping("/getResourceById")
    public void getResourceById(ResourceEntity resource, HttpServletResponse response){
        super.ajaxJson(response, resourceService.queryObjectById(resource.getResourceCode()));
    }
    /**
     * <p>Description: 删除系统资源记录</p>
     * @param resource 系统资源对象
     * @param response
     */
    @RequestMapping("deleteResource")
    public void deleteResource(ResourceEntity resource, HttpServletResponse response){
    	log.info("用户"+getSessionUser().getUserName()+"删除了系统资源:"+(new Gson()).toJson(resource));
    	resource.setUpdateUser(getSessionUser().getUserId());
        resource.setUpdateTime(new Date());
        resourceService.delete(resource);
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    
    /**
     * <p>Description: 查看系统资源明细</p>
     * @param resource 系统资源对象
     * @param model
     * @return
     */
    @RequestMapping("viewResourceDetail")
    public String viewResourceDetail(ResourceEntity resource, Model model){
        model.addAttribute("resourceCode",resource.getResourceCode());
        return "resource/resourceDetail";
    }
}
